package com.event.service.impl;

import com.event.mapper.ArticleMapper;
import com.event.pojo.Article;
import com.event.pojo.PageBean;
import com.event.service.ArticleService;
import com.event.utils.ThreadLocalUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public void addArticleAction(Article article) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        article.setCreateUser(id);
        articleMapper.addArticleAction(article);
    }

    @Override
    public PageBean<Article> queryArticleList(Integer pageNum, Integer pageSize, Integer categoryId, String state) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        //创建PageBean对象
        PageBean<Article> article = new PageBean<>();
        //开启分页查询
        PageHelper.startPage(pageNum, pageSize);

        //调用Mapper
        List<Article> articleList = articleMapper.queryArticleList(categoryId,state,userId);
        //page中提供了方法，可以获取PageHelper分页查询后，得到的总记录条数和当前页数据
        Page<Article> as = (Page<Article>)articleList;
        //把数据填充到PageBean对象中
        article.setTotal(as.getTotal());
        article.setItems(as.getResult());
        return article;
    }
}
